package main;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import moteur.Moteur;
import moteur.NormalSearch;
import moteur.Search;

/**
 * 				Entry of the program
 * 
 * 1. Firstly, check whether the index files already exist
 * 2. If yes, construct index directly from the files
 * 3. If no, construct index from the files, the directory of which the user should provide
 *
 * 4. After constructing the index, enter the main loop:
 * 	  I. 	The user choose whether 'Boolean' or 'Normal' search
 * 	  II.	Search input
 *
 */
public class Main 
{
	public static void main(String[] args) throws IOException
	{
		Moteur m = new Moteur();
		BufferedReader ins=new BufferedReader( new InputStreamReader(System.in));
		if(!TestFileExistence("Index.txt"))
		{
			System.out.println("Input the corpus Path:");
			String path=ins.readLine();
			System.out.println("	Indexing......");
			m.constructIndex(path);
			m.printIndex("Index.txt");
		}
		else
		{
			System.out.println("	Indexing......");
			m.constructIndexFromFile("Index.txt");
		}
		System.out.println("++++++indexing finished++++++++");
		//System.out.println("output file: stemmer_Index.txt, tokenizer_Index.txt file_id_map.txt");
		while(true)
		{
			System.out.println("\n\n");
			System.out.println("===========================================");
			//Choosse the search type: boolean search or normal search
			System.out.println("Boolean Search:[Y|N]");
			String booleanSearch= ins.readLine().toLowerCase();
			if(booleanSearch.contains("y"))
				m.setBoolSearch(true);
			else 
			{
				if(booleanSearch.contains("n"))
					m.setBoolSearch(false);
				else{
					System.out.println("illegal input");
					continue;
				}
			}
			System.out.println("INPUT:");
			String str= ins.readLine();
			ArrayList<String> in = new ArrayList<String>();
			boolean legal=m.parseInput(str,in);
			if(!legal)
			{
				System.out.println("Illegal input search");
				continue;
			}
			System.out.println("Search terms :"+in);
			if(m.isBoolSearch()==false)
			{
				NormalSearch ns=(NormalSearch) m.setupSearch(in);
				System.out.println("show at least how many results:");
				String number= ins.readLine().trim();
				ns.setNbResults(Integer.parseInt(number));
				ns.print();
				System.out.println("Search Results:");
				System.out.println(ns.get());
			}
			else
			{
				Search s = m.setupSearch(in);
				if(s != null)
				{
					s.print();
					System.out.println("Search Results:");
					System.out.println(s.get());
				}
			}//endelse
		}//endwhile
	}
	
	//Check whether the index files exist
	public static boolean TestFileExistence(String fileName)
	{
		String stemmer = "stemmer_"+fileName;
		String tokenizer = "tokenizer_"+fileName;
		File fs = new File(stemmer);
		File ft = new File(tokenizer);
		
		return fs.exists() && ft.exists();
	}
}
